package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.model.User;
import com.myblog.service.CommentService;
import com.myblog.service.LikeService;
import com.myblog.service.UserService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Slf4j
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;


    /**
     * 获取最新评论
     * @param rows
     * @param pageNum
     * @return
     */
    @GetMapping("/newComment")
    public String getNewComment(@RequestParam(value = "rows") Integer rows,@RequestParam(value = "pageNum") Integer pageNum){
        try {
            DataMap data = commentService.getNewCommment(rows,pageNum);
            return JsonResult.build(data).toJSON();
        } catch (Exception e) {
            log.error("CommentController getNewComment Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    @PostMapping("/getUserComment")
    public String getUserComment(@RequestParam(value = "rows") Integer rows, @RequestParam(value = "pageNum") Integer pageNum,
                                 HttpServletRequest request){
        try {
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            Integer userId = user.getId();
            DataMap data = commentService.getUserCommment(rows,pageNum,userId);
            return JsonResult.build(data).toJSON();
        } catch (Exception e) {
            log.error("CommentController getUserComment Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    /**
     * 根据文章获取评论
     * @param articleIdString
     * @return
     */
    @PostMapping("/getAllComment")
    public String getPageLeaveMessage(@RequestParam(value = "articleId")String articleIdString,HttpServletRequest request){
        try{
            long articleId = Long.parseLong(articleIdString);
            //获取点赞人
            Principal userPrincipal = request.getUserPrincipal();
            String userName = null;
            //判断用户是否已经点赞了评论
            if(userPrincipal != null){
                userName = userPrincipal.getName();
            }
            //获取评论信息
            DataMap data = commentService.getCommentByArticleId(articleId,userName);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CommentController getPageLeaveMessage Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 发表评论
     * @param commentContent
     * @param articleIdString
     * @param request
     * @return
     */
    @PostMapping("/publishComment")
    public String publishComment(@RequestParam(value = "commentContent")String commentContent,
                                      @RequestParam(value = "articleId")String articleIdString,
                                      HttpServletRequest request){
        try{
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            long articleId = Long.parseLong(articleIdString);
            User user = userService.searchUserByName(userPrincipal.getName());
            int answerId = user.getId();
            DataMap data = commentService.publishCommentMessage(commentContent,articleId,answerId);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CommentController publishComment Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 对评论回复
     * @return
     */
    @PostMapping("/publishReply")
    public String publishLeaveMessageReply(@RequestParam(value = "commentContent")String commentContent,
                                           @RequestParam(value = "articleId")String articleIdString,
                                           @RequestParam(value = "parentId")String pIdString,
                                           @RequestParam(value = "respondent")String respondentName,
                                           HttpServletRequest request){
        try{
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            //将获取到的文章id转换格式
            long articleId = Long.parseLong(articleIdString);
            int answerId = user.getId();
            DataMap data = commentService.ReplyComment(commentContent,articleId,answerId,pIdString,respondentName);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CommentController findAllCategoriesName Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
